import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';

class LoginScreen extends StatefulWidget {
  const LoginScreen({super.key});

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  bool isPhoneLogin = true; // 控制显示手机号登录还是其他方式登录
  String selectedRegion = '中国大陆'; // 默认选中的地区
  final phoneController = TextEditingController();
  final accountController = TextEditingController();
  final passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () {
            // 处理返回逻辑
          },
        ),
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 20.h),
            Text(
              isPhoneLogin ? '手机号登录' : '微信号/QQ号/邮箱登录',
              style: TextStyle(
                fontSize: 24.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: 30.h),
            if (isPhoneLogin) _buildPhoneLogin() else _buildAccountLogin(),
            const Spacer(),
            _buildBottomButton(),
            SizedBox(height: 16.h),
            _buildBottomActions(),
            SizedBox(height: 40.h),
          ],
        ),
      ),
    );
  }

  Widget _buildPhoneLogin() {
    return Column(
      children: [
        // 国家/地区选择
        InkWell(
          onTap: () {
            // 处理地区选择
          },
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 12.h),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Colors.grey, width: 0.5),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('国家/地区'),
                Row(
                  children: [
                    Text(selectedRegion),
                    Icon(Icons.chevron_right),
                  ],
                ),
              ],
            ),
          ),
        ),
        // 手机号输入
        Container(
          padding: EdgeInsets.symmetric(vertical: 8.h),
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.grey, width: 0.5),
            ),
          ),
          child: Row(
            children: [
              Text('+86'),
              SizedBox(width: 10.w),
              Expanded(
                child: TextField(
                  controller: phoneController,
                  decoration: const InputDecoration(
                    hintText: '请填写手机号码',
                    border: InputBorder.none,
                  ),
                  keyboardType: TextInputType.phone,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildAccountLogin() {
    return Column(
      children: [
        // 账号输入
        Container(
          padding: EdgeInsets.symmetric(vertical: 8.h),
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.grey, width: 0.5),
            ),
          ),
          child: TextField(
            controller: accountController,
            decoration: const InputDecoration(
              hintText: '微信号/QQ号/邮箱',
              border: InputBorder.none,
            ),
          ),
        ),
        // 密码输入
        Container(
          padding: EdgeInsets.symmetric(vertical: 8.h),
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.grey, width: 0.5),
            ),
          ),
          child: TextField(
            controller: passwordController,
            decoration: const InputDecoration(
              hintText: '请填写密码',
              border: InputBorder.none,
            ),
            obscureText: true,
          ),
        ),
      ],
    );
  }

  Widget _buildBottomButton() {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        onPressed: () {
          // 处理登录逻辑
          // TODO: 添加实际的登录验证

          // 登录成功后跳转到主页面
          context.go('/main'); // 修改这里，导航到 MainScreen
        },
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.green,
          padding: EdgeInsets.symmetric(vertical: 12.h),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(4),
          ),
        ),
        child: Text(
          isPhoneLogin ? '同意并继续' : '同意并登录',
          style: TextStyle(
            fontSize: 16.sp,
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  Widget _buildBottomActions() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        TextButton(
          onPressed: () {
            // 处理找回密码
          },
          child: const Text(
            '找回密码',
            style: TextStyle(color: Colors.grey),
          ),
        ),
        TextButton(
          onPressed: () {
            setState(() {
              isPhoneLogin = !isPhoneLogin;
            });
          },
          child: Text(
            isPhoneLogin ? '用微信号/QQ号/邮箱登录' : '用手机号登录',
            style: const TextStyle(color: Colors.grey),
          ),
        ),
      ],
    );
  }
}
